package com.cop4331.social;

import java.util.ArrayList;
import java.util.HashMap;

import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.Spinner;
import android.widget.TextView;

/**
 * 
 * @author Matt Szoke
 * @LastUpdated: 3/5/2012 by Matt Szoke
 * 
 * Prompts for user that is requesting tutor
 * 
 */
public class NeedTutorActivity extends Activity {

	Spinner courseList;
	Button ok;
	Button cancel;
	TextView text;
	public static String un;
	public static String term;

	// -----------------------------------------------
	// ONCREATE
	// -----------------------------------------------
	public void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		setContentView(R.layout.cantutor);

		// Spinner
		courseList = (Spinner) findViewById(R.id.s_courselist); // Contains
																// courselists
																// available to
																// be tutored in

		// User Instructions
		text = (TextView) findViewById(R.id.tv_text);

		// Buttons
		ok = (Button) findViewById(R.id.b_ok);
		cancel = (Button) findViewById(R.id.b_cancel);

		// Arraylist to be used with ArrayAdapter (Holds user courselist)
		ArrayList<String> spinnerArray = new ArrayList<String>();

		// ArrayAdapter - uses spintest.xml for formatting, stores spinnerArray
		// values(ArrayList)
		ArrayAdapter spinnerArrayAdapter = new ArrayAdapter(this,
				R.layout.spintest, spinnerArray);

		// Retreive username and current term
		DatabaseHandler db = new DatabaseHandler(getApplicationContext());
		HashMap<String, String> user = db.getUserDetails();
		un = user.get("name");
		term = user.get("uid");

		// Show instructions to user
		text.setText("Choose which course you would like to be tutored in from your available list:");

		UserFunctions uf = new UserFunctions();

		// Retreive all courses user is currently/has been enrolled in
		JSONObject json = uf.retrieveAllCourses(un);

		// Holds size of class list
		int size;

		try {
			size = Integer.parseInt(json.getString("size"));

			// Parse list and add to spinner relevant courses
			for (int i = 0; i < size; i++) {
				try {

					// Each course in the Object is referenced from a String
					// number (i.e "1", "2", "3")
					JSONObject json_course = json.getJSONObject("" + i);

					// Add courses based on the following:
					// -Term is equal to current term
					if (json_course.getString("term").compareTo(term) == 0)
						spinnerArray.add(json_course.getString("cname"));

				} catch (JSONException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}

			// We have no courses to display (Nothing to tutor), remove OK
			// button visibility, user can only cancel
			if (spinnerArray.size() == 0) {
				spinnerArray.add("           No Classes Available");
				ok.setVisibility(View.GONE);
				courseList.setClickable(false);
			}

			// Add courses to adapter now
			courseList.setAdapter(spinnerArrayAdapter);

		} catch (NumberFormatException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		} catch (JSONException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}

		// Add functionality to "Select this course" button
		ok.setOnClickListener(new View.OnClickListener() {

			public void onClick(View v) {

				UserFunctions uf = new UserFunctions();

				// Update the database setting Tutoring to 0, you can not tutor
				// a course you're being tutored in
				JSONObject jsonUp = uf.updateTutor(un, courseList
						.getSelectedItem().toString(), term, "0");

				// Bundle course name for ChooseTutor Activity
				Bundle b = new Bundle();
				b.putString("cname", courseList.getSelectedItem().toString());

				// Create intent and add extras
				Intent i = new Intent(getApplicationContext(),
						ChooseTutorActivity.class);
				i.putExtras(b);

				// Start ChooseTutorActivity
				startActivity(i);
				finish();

			}
		});

		cancel.setOnClickListener(new View.OnClickListener() {

			public void onClick(View v) {

				// Return to dashboard
				Intent log = new Intent(getApplicationContext(),
						DashBoardActivity.class);
				startActivity(log);
				finish();

			}
		});
		
		// Title Bar Buttons
		ImageButton about, home, message;

		about = (ImageButton) findViewById(R.id.b_about);
		home = (ImageButton) findViewById(R.id.b_home);
		message = (ImageButton) findViewById(R.id.b_message);
		
		// Check for new messages
		JSONObject j_mess = uf.retreiveMessages(un, "new");

		try {
			if (Integer.parseInt(j_mess.getString("size")) > 0) {
				message.setVisibility(View.VISIBLE);
			} else
				message.setVisibility(View.GONE);
		} catch (NumberFormatException e) {
			e.printStackTrace();
		} catch (JSONException e) {
			e.printStackTrace();
		}
		
		// Show app info
		about.setOnClickListener(new View.OnClickListener() {

			public void onClick(View v) {

				Dialog dialog = new Dialog(NeedTutorActivity.this);

				dialog.setContentView(R.layout.help_dialog);
				dialog.setTitle("CramSessions v 1.0 2012");
				dialog.setCancelable(true);

				// Create and show a dialog to help the user with this specific
				// field
				TextView text = (TextView) dialog.findViewById(R.id.text);
				text.setText("CramSessions was created by:\n\n-Matt Szoke\n-Daniel Moody\n-Jeovanny Alvarez\n-Kia Manoochehri\n-Rj Anderson\n-Doug Wagner");
				text.setGravity(Gravity.LEFT);
				dialog.setCanceledOnTouchOutside(true);
				dialog.show();
			}
		});
	
		// Return to dashboard
		home.setOnClickListener(new View.OnClickListener() {

			public void onClick(View v) {

				Intent sa = new Intent(getApplicationContext(),
						DashBoardActivity.class);
				startActivity(sa);
				finish();

			}
		});
		
		// Goto inbox if new message exists
		message.setOnClickListener(new View.OnClickListener() {

			public void onClick(View v) {

				Intent sa = new Intent(getApplicationContext(),
						MessageNotifications.class);
				startActivity(sa);
				finish();

			}
		});
	}
}
